<?php

class Search extends Controller {

    function Search() {
        parent::Controller();
        $this->load->model('search_model');
    }

    function index() {
        $this->populateSearchPage();
    }

    function getAllStates() {
        $stateId = array();
        $stateDescription = array();
        $result = $this->search_model->getAllStates();

        if (count($result) > 0) {
            foreach ($result as $row) {
                $stateId[] = $row['state_id'];
                $stateDescription[] = $row['description'];
            }
        }
        $combinedArray = array('stateId' => $stateId, 'stateDescription' => $stateDescription);
        return $combinedArray;
    }

    function getLCDsByState() {
        $stateId = $_POST['stateId'];
        if ($stateId != '') {
            $stateName = $this->getStateNameByStateId($stateId);

            $lcdId = array();
            $lcdTitle = array();
            $contractorName = array();
            $title = '';
            $contractorIdList = '';

            $result = $this->getUniqueResultArray($this->search_model->searchByState($stateId));

            if (count($result) > 0) {
                foreach ($result as $row) {
                    $lcdId[] = $row['lcd_id'];
                    $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                    $contractorName[] = $row['contractor_name'];
                }
            }

            $combinedArrayFromGetLCDsByState = array(
                'lcdId' => $lcdId,
                'lcdTitle' => $lcdTitle,
                'contractorName' => $contractorName,
                'stateName' => $stateName,
                'title' => $title,
                'contractorIdList' => $contractorIdList);

            $combinedArrayFromGetContractorsByContractorType = '';
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByContractorIds = '';

            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle);
        } else {
            $this->populateSearchPage();
        }
    }

    function getLCDsByTitle() {
        $lcdId = array();
        $lcdTitle = array();
        $contractorName = array();
        $stateName = '';
        $contractorIdList = '';

        $title = $_POST['title'];

        if ($title != '') {
            $result = $this->getUniqueResultArray($this->search_model->searchByTitle($title));
            if (count($result) > 0) {
                foreach ($result as $row) {
                    $lcdId[] = $row['lcd_id'];
                    $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                    $contractorName[] = $row['contractor_name'];
                }
            }
        }
        $combinedArrayFromGetLCDsByTitle = array(
            'lcdId' => $lcdId,
            'lcdTitle' => $lcdTitle,
            'contractorName' => $contractorName,
            'stateName' => $stateName,
            'title' => $title,
            'contractorIdList' => $contractorIdList);

        $combinedArrayFromGetContractorsByContractorType = '';
        $combinedArrayFromGetLCDsByState = '';
        $combinedArrayFromGetLCDsByContractorIds = '';

        $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle);
    }

    function getAllContractorTypes() {
        $contractorTypeId = array();
        $contractorTypeDescription = array();
        $result = $this->search_model->getContractorTypes();

        if (count($result) > 0) {
            foreach ($result as $row) {
                $contractorTypeId[] = $row['contractor_type_id'];
                $contractorTypeDescription[] = $row['description'];
            }
        }
        $combinedArray = array('contractorTypeId' => $contractorTypeId, 'contractorTypeDescription' => $contractorTypeDescription);
        return $combinedArray;
    }

    function getContractorsByContractorType() {
        $contractorTypeId = $_POST['contractorType'];
        if ($contractorTypeId != '') {
            $contractorId = array();
            $contractorNumber = array();
            $contractorNameList = array();
            $result = array();
            $combinedArrayFromGetContractorsByContractorType = '';
            $combinedArrayFromGetLCDsByState = '';
            $combinedArrayFromGetLCDsByTitle = '';

            $result = $this->search_model->getContractorByContractorType($contractorTypeId);

            if (count($result) > 0) {
                foreach ($result as $row) {
                    $contractorId[] = $row['contractor_id'];
                    $contractorNumber[] = $row['contractor_number'];
                    $contractorNameList[] = $row['contractor_name'];
                }
            }

            $combinedArrayFromGetContractorsByContractorType = array('contractorId' => $contractorId, 'contractorNumber' => $contractorNumber, 'contractorNameList' => $contractorNameList, 'contractorTypeId' => $contractorTypeId);
            $combinedArrayFromGetLCDsByState = '';
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByContractorIds = '';

            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle);
        }
        else
            $this->populateSearchPage();
    }

    function getLCDsByContractorId() {

        $contractorIdList = $_POST['contractorNumbersAndNames'];
        $contractorTypeId = $_POST['hiddenContractorTypeId'];
        if (count($contractorIdList) > 0) {
            $lcdId = array();
            $lcdTitle = array();
            $contractorName = array();
            $title = '';
            $stateName = '';

            $result = $this->search_model->searchByContractorIds($contractorIdList);

            if (count($result) > 0) {
                foreach ($result as $row) {
                    $lcdId[] = $row['lcd_id'];
                    $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                    $contractorName[] = $row['contractor_name'];
                }
            }
            $combinedArrayFromGetLCDsByContractorIds = array(
                'lcdId' => $lcdId,
                'lcdTitle' => $lcdTitle,
                'contractorName' => $contractorName,
                'stateName' => $stateName,
                'title' => $title,
                'contractorIdList' => $contractorIdList);

            $result = $this->search_model->getContractorByContractorType($contractorTypeId);

            if (count($result) > 0) {
                foreach ($result as $row) {
                    $contractorId[] = $row['contractor_id'];
                    $contractorNumber[] = $row['contractor_number'];
                    $contractorNameList[] = $row['contractor_name'];
                }
            }
            $combinedArrayFromGetContractorsByContractorType = array('contractorId' => $contractorId, 'contractorNumber' => $contractorNumber, 'contractorNameList' => $contractorNameList, 'contractorTypeId' => $contractorTypeId);
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByState = '';
            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle, $combinedArrayFromGetLCDsByContractorIds);
        }
    }

    /*
      function getLCDsByStateAndTitle_Old() {
      $lcdId = array();
      $lcdTitle = array();
      $contractorName = array();
      $contractorIdList = '';
      $stateName = '';
      $stateId = $_POST['hiddenStateId'];
      $title = $_POST['hiddenTitle'];

      if ($stateId != '' && $title != '') {
      $result = $this->search_model->searchByStateAndTitle($stateId, $title);
      $stateName = $this->getStateNameByStateId($stateId);

      if (count($result) > 0) {
      foreach ($result as $row) {
      $lcdId[] = $row['lcd_id'];
      $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
      $contractorName[] = $row['contractor_name'];
      }
      }
      }
      $combinedArrayFromGetLCDsByTitle = array(
      'lcdId' => $lcdId,
      'lcdTitle' => $lcdTitle,
      'contractorName' => $contractorName,
      'stateName' => $stateName,
      'title' => $title,
      'contractorIdList' => $contractorIdList);

      $combinedArrayFromGetContractorsByContractorType = '';
      $combinedArrayFromGetLCDsByState = '';
      $combinedArrayFromGetLCDsByContractorIds = '';

      $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle);
      }
     */

    function getLCDsByStateAndTitle() {
        $lcdId = array();
        $lcdTitle = array();
        $contractorName = array();
        $contractorIdList = '';
        $stateName = '';
        $stateId = $_POST['hiddenStateId'];
        $title = $_POST['hiddenTitle'];
        if ($stateId != '' && $title != '') {
            //$result = $this->search_model->searchByStateAndTitle($stateId, $title);
            $stateName = $this->getStateNameByStateId($stateId);

            $result1 = $this->getUniqueResultArray($this->search_model->searchByState($stateId));
            $result2 = $this->getUniqueResultArray($this->search_model->searchByTitle($title));
            $result = $this->getIntersectionArray($result1, $result2);
            if (count($result) > 0) {
                foreach ($result as $row) {
                    $lcdId[] = $row['lcd_id'];
                    $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                    $contractorName[] = $row['contractor_name'];
                }
            }
        }
        $combinedArrayFromGetLCDsByTitle = array(
            'lcdId' => $lcdId,
            'lcdTitle' => $lcdTitle,
            'contractorName' => $contractorName,
            'stateName' => $stateName,
            'title' => $title,
            'contractorIdList' => $contractorIdList);

        $combinedArrayFromGetContractorsByContractorType = '';
        $combinedArrayFromGetLCDsByState = '';
        $combinedArrayFromGetLCDsByContractorIds = '';

        $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle);
    }

    function getLCDsByTitleAndContractorId() {

        $contractorTypeId = $_POST['hiddenContractorTypeId2'];
        $contractorIdList = explode(",", $_POST['hiddenContractorIds2'][0]);
        $title = $_POST['hiddenTitle2'];

        if (count($contractorIdList) > 0) {
            $lcdId = array();
            $lcdTitle = array();
            $contractorName = array();
            $contractorId = array();
            $contractorNumber = array();
            $contractorNameList = array();

            $stateName = '';

            if ($title != '' && $contractorIdList != '') {
                //$result = $this->search_model->searchByTitleAndContractorIds($title, $contractorIdList);
                $result1 = $this->getUniqueResultArray($this->search_model->searchByContractorIds($contractorIdList));
                $result2 = $this->getUniqueResultArray($this->search_model->searchByTitle($title));
                $result = $this->getIntersectionArray($result1, $result2);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $lcdId[] = $row['lcd_id'];
                        $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                        $contractorName[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetLCDsByContractorIds = array(
                'lcdId' => $lcdId,
                'lcdTitle' => $lcdTitle,
                'contractorName' => $contractorName,
                'stateName' => $stateName,
                'title' => $title,
                'contractorIdList' => $contractorIdList);
            if ($contractorTypeId != '') {
                $result = $this->search_model->getContractorByContractorType($contractorTypeId);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $contractorId[] = $row['contractor_id'];
                        $contractorNumber[] = $row['contractor_number'];
                        $contractorNameList[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetContractorsByContractorType = array('contractorId' => $contractorId, 'contractorNumber' => $contractorNumber, 'contractorNameList' => $contractorNameList, 'contractorTypeId' => $contractorTypeId);
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByState = '';
            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle, $combinedArrayFromGetLCDsByContractorIds);
        }
    }

    function getLCDsByStateAndContractorId() {

        $contractorIdList = '';
        $contractorTypeId = $_POST['hiddenContractorTypeId3'];
        if ($_POST['hiddenContractorIds3'][0] != '')
            $contractorIdList = explode(",", $_POST['hiddenContractorIds3'][0]);
        $stateId = $_POST['hiddenState3'];

        if (count($contractorIdList) > 0) {
            $lcdId = array();
            $lcdTitle = array();
            $contractorName = array();
            $contractorId = array();
            $contractorNumber = array();
            $contractorNameList = array();
            $title = '';
            $stateName = '';
            if ($stateId != '' && $contractorIdList != '') {
                $stateName = $this->getStateNameByStateId($stateId);
                //$result = $this->search_model->searchByStateAndContractorIds($stateId, $contractorIdList);
                $result1 = $this->getUniqueResultArray($this->search_model->searchByContractorIds($contractorIdList));
                $result2 = $this->getUniqueResultArray($this->search_model->searchByState($stateId));
                $result = $this->getIntersectionArray($result1, $result2);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $lcdId[] = $row['lcd_id'];
                        $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                        $contractorName[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetLCDsByContractorIds = array(
                'lcdId' => $lcdId,
                'lcdTitle' => $lcdTitle,
                'contractorName' => $contractorName,
                'stateName' => $stateName,
                'title' => $title,
                'contractorIdList' => $contractorIdList);
            if ($contractorTypeId != '') {
                $result = $this->search_model->getContractorByContractorType($contractorTypeId);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $contractorId[] = $row['contractor_id'];
                        $contractorNumber[] = $row['contractor_number'];
                        $contractorNameList[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetContractorsByContractorType = array('contractorId' => $contractorId, 'contractorNumber' => $contractorNumber, 'contractorNameList' => $contractorNameList, 'contractorTypeId' => $contractorTypeId);
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByState = '';
            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle, $combinedArrayFromGetLCDsByContractorIds);
        }
    }

    function getLCDsByStateAndTitleAndContractorId() {

        $contractorTypeId = $_POST['hiddenContractorTypeId4'];
        $contractorIdList = explode(",", $_POST['hiddenContractorIds4'][0]);
        $stateId = $_POST['hiddenState4'];
        $title = $_POST['hiddenTitle4'];

        if (count($contractorIdList) > 0) {
            $lcdId = array();
            $lcdTitle = array();
            $contractorName = array();
            $contractorId = array();
            $contractorNumber = array();
            $contractorNameList = array();
            $stateName = '';

            if ($stateId != '' && $contractorIdList != '' && $title != '') {
                $stateName = $this->getStateNameByStateId($stateId);
                //$result = $this->search_model->searchByStateAndTitleAndContractorIds($stateId, $title, $contractorIdList);

                $result1 = $this->getUniqueResultArray($this->search_model->searchByContractorIds($contractorIdList));
                $result2 = $this->getUniqueResultArray($this->search_model->searchByTitle($title));
                $result3 = $this->getUniqueResultArray($this->search_model->searchByState($stateId));

                $result4 = $this->getIntersectionArray($result1, $result2);
                $result = $this->getIntersectionArray($result3, $result4);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $lcdId[] = $row['lcd_id'];
                        $lcdTitle[] = $this->getLCDTitle($row['lcd_id']);
                        $contractorName[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetLCDsByContractorIds = array(
                'lcdId' => $lcdId,
                'lcdTitle' => $lcdTitle,
                'contractorName' => $contractorName,
                'stateName' => $stateName,
                'title' => $title,
                'contractorIdList' => $contractorIdList);
            if ($contractorTypeId != '') {
                $result = $this->search_model->getContractorByContractorType($contractorTypeId);

                if (count($result) > 0) {
                    foreach ($result as $row) {
                        $contractorId[] = $row['contractor_id'];
                        $contractorNumber[] = $row['contractor_number'];
                        $contractorNameList[] = $row['contractor_name'];
                    }
                }
            }
            $combinedArrayFromGetContractorsByContractorType = array('contractorId' => $contractorId, 'contractorNumber' => $contractorNumber, 'contractorNameList' => $contractorNameList, 'contractorTypeId' => $contractorTypeId);
            $combinedArrayFromGetLCDsByTitle = '';
            $combinedArrayFromGetLCDsByState = '';
            $this->populateSearchPage($combinedArrayFromGetContractorsByContractorType, $combinedArrayFromGetLCDsByState, $combinedArrayFromGetLCDsByTitle, $combinedArrayFromGetLCDsByContractorIds);
        }
    }

    function populateSearchPage($combinedArrayFromGetContractorsByContractorType = '', $combinedArrayFromGetLCDsByState = '', $combinedArrayFromGetLCDsByTitle = '', $combinedArrayFromGetLCDsByContractorIds='') {
        $lcdId = array();
        $lcdTitle = array();
        $contractorName = array();
        $contractorId = array();
        $contractorNumber = array();
        $contractorNameList = array();
        $stateId = array();
        $stateDescription = array();
        $contractorTypeId = array();
        $contractorTypeDescription = array();
        $contractorTypeIdSelected = '';
        $contractorType = '';
        $stateName = '';
        $title = '';
        $contractorIdList = '';


        /* Get All States' Data and catagorize it in ID & STATE NAME */
        $combinedStatesArray = $this->getAllStates();
        foreach ($combinedStatesArray['stateId'] as $row) {
            $stateId[] = $row;
        }
        foreach ($combinedStatesArray['stateDescription'] as $row) {
            $stateDescription[] = $row;
        }

        /* Get All Contractor Types' Data and catagorize it in ID & CONTRACTOR TYPE */
        $combinedContractorTypesArray = $this->getAllContractorTypes();
        foreach ($combinedContractorTypesArray['contractorTypeId'] as $row) {
            $contractorTypeId[] = $row;
        }
        foreach ($combinedContractorTypesArray['contractorTypeDescription'] as $row) {
            $contractorTypeDescription[] = $row;
        }

        /* First 3 Conditions - Get all contractors' ID, CONTRACTOR NUMBER and CONTRACTOR NAME for selected contractor type and Populate List */
        if ($combinedArrayFromGetContractorsByContractorType != '') {
            if (count($combinedArrayFromGetContractorsByContractorType['contractorId']) > 0) {
                foreach ($combinedArrayFromGetContractorsByContractorType['contractorId'] as $row)
                    $contractorId[] = $row;
            }
            if (count($combinedArrayFromGetContractorsByContractorType['contractorNumber']) > 0) {
                foreach ($combinedArrayFromGetContractorsByContractorType['contractorNumber'] as $row)
                    $contractorNumber[] = $row;
            }
            if (count($combinedArrayFromGetContractorsByContractorType['contractorNameList']) > 0) {
                foreach ($combinedArrayFromGetContractorsByContractorType['contractorNameList'] as $row)
                    $contractorNameList[] = $row;
            }
            $contractorTypeIdSelected = $combinedArrayFromGetContractorsByContractorType['contractorTypeId'];
        }

        /* Next 7 Conditions - populate the search results for LCDs by State */
        if ($combinedArrayFromGetLCDsByState != '') {
            if (count($combinedArrayFromGetLCDsByState['lcdId']) > 0) {
                foreach ($combinedArrayFromGetLCDsByState['lcdId'] as $row)
                    $lcdId[] = $row;
            }
            if (count($combinedArrayFromGetLCDsByState['lcdTitle']) > 0) {
                foreach ($combinedArrayFromGetLCDsByState['lcdTitle'] as $row)
                    $lcdTitle[] = $row;
            }
//            if (count($combinedArrayFromGetLCDsByState['lcdIndication']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdIndication'] as $row)
//                    $lcdIndication[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByState['lcdDocReqs']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdDocReqs'] as $row)
//                    $lcdDocReqs[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByState['lcdAdvMeeting']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdAdvMeeting'] as $row)
//                    $lcdAdvMeeting[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByState['lcdHistoryExp']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdHistoryExp'] as $row)
//                    $lcdHistoryExp[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByState['lcdCmcCovPolicy']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdCmcCovPolicy'] as $row)
//                    $lcdCmcCovPolicy[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByState['lcdUtilGuide']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByState['lcdUtilGuide'] as $row)
//                    $lcdUtilGuide[] = $row;
//            }
            if (count($combinedArrayFromGetLCDsByState['contractorName']) > 0) {
                foreach ($combinedArrayFromGetLCDsByState['contractorName'] as $row)
                    $contractorName[] = $row;
            }
            $stateName = $combinedArrayFromGetLCDsByState['stateName'];
            $title = $combinedArrayFromGetLCDsByState['title'];
            $contractorIdList = $combinedArrayFromGetLCDsByState['contractorIdList'];
        }

        /* Next 7 Conditions - populate the search results for LCDs by Title */
        if ($combinedArrayFromGetLCDsByTitle != '') {
            if (count($combinedArrayFromGetLCDsByTitle['lcdId']) > 0) {
                foreach ($combinedArrayFromGetLCDsByTitle['lcdId'] as $row)
                    $lcdId[] = $row;
            }
            if (count($combinedArrayFromGetLCDsByTitle['lcdTitle']) > 0) {
                foreach ($combinedArrayFromGetLCDsByTitle['lcdTitle'] as $row)
                    $lcdTitle[] = $row;
            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdIndication']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdIndication'] as $row)
//                    $lcdIndication[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdDocReqs']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdDocReqs'] as $row)
//                    $lcdDocReqs[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdAdvMeeting']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdAdvMeeting'] as $row)
//                    $lcdAdvMeeting[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdHistoryExp']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdHistoryExp'] as $row)
//                    $lcdHistoryExp[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdCmcCovPolicy']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdCmcCovPolicy'] as $row)
//                    $lcdCmcCovPolicy[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByTitle['lcdUtilGuide']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByTitle['lcdUtilGuide'] as $row)
//                    $lcdUtilGuide[] = $row;
//            }
            if (count($combinedArrayFromGetLCDsByTitle['contractorName']) > 0) {
                foreach ($combinedArrayFromGetLCDsByTitle['contractorName'] as $row)
                    $contractorName[] = $row;
            }
            $stateName = $combinedArrayFromGetLCDsByTitle['stateName'];
            $title = $combinedArrayFromGetLCDsByTitle['title'];
            $contractorIdList = $combinedArrayFromGetLCDsByTitle['contractorIdList'];
        }

        /* Next 7 Conditions - populate the search results for LCDs by Contractor Ids */
        if ($combinedArrayFromGetLCDsByContractorIds != '') {
            if (count($combinedArrayFromGetLCDsByContractorIds['lcdId']) > 0) {
                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdId'] as $row)
                    $lcdId[] = $row;
            }
            if (count($combinedArrayFromGetLCDsByContractorIds['lcdTitle']) > 0) {
                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdTitle'] as $row)
                    $lcdTitle[] = $row;
            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdIndication']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdIndication'] as $row)
//                    $lcdIndication[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdDocReqs']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdDocReqs'] as $row)
//                    $lcdDocReqs[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdAdvMeeting']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdAdvMeeting'] as $row)
//                    $lcdAdvMeeting[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdHistoryExp']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdHistoryExp'] as $row)
//                    $lcdHistoryExp[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdCmcCovPolicy']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdCmcCovPolicy'] as $row)
//                    $lcdCmcCovPolicy[] = $row;
//            }
//            if (count($combinedArrayFromGetLCDsByContractorIds['lcdUtilGuide']) > 0) {
//                foreach ($combinedArrayFromGetLCDsByContractorIds['lcdUtilGuide'] as $row)
//                    $lcdUtilGuide[] = $row;
//            }
            if (count($combinedArrayFromGetLCDsByContractorIds['contractorName']) > 0) {
                foreach ($combinedArrayFromGetLCDsByContractorIds['contractorName'] as $row)
                    $contractorName[] = $row;
            }
            $stateName = $combinedArrayFromGetLCDsByContractorIds['stateName'];
            $title = $combinedArrayFromGetLCDsByContractorIds['title'];
            $contractorIdList = $combinedArrayFromGetLCDsByContractorIds['contractorIdList'];
        }

        $this->load->view('search_view', array(
            'lcdId' => $lcdId,
            'lcdTitle' => $lcdTitle,
//            'lcdIndication' => $lcdIndication,
//            'lcdDocReqs' => $lcdDocReqs,
//            'lcdAdvMeeting' => $lcdAdvMeeting,
//            'lcdHistoryExp' => $lcdHistoryExp,
//            'lcdCmcCovPolicy' => $lcdCmcCovPolicy,
//            'lcdUtilGuide' => $lcdUtilGuide,
            'contractorName' => $contractorName,
            'stateName' => $stateName,
            'title' => '',
            'stateId' => $stateId,
            'stateDescription' => $stateDescription,
            'contractorTypeId' => $contractorTypeId,
            'contractorTypeDescription' => $contractorTypeDescription,
            'contractorId' => $contractorId,
            'contractorTypeId' => $contractorTypeId,
            'contractorType' => $contractorType,
            'contractorTypeIdSelected' => $contractorTypeIdSelected,
            'contractorNumber' => $contractorNumber,
            'contractorNameList' => $contractorNameList,
            'stateName' => $stateName,
            'title' => $title,
            'contractorIdList' => $contractorIdList));
    }

    function showLCDInformation($lcdIdNo) {
        $lcdId = array();
        $lcdTitle = array();
        $lcdIndication = array();
        $lcdDocReqs = array();
        $lcdAdvMeeting = array();
        $lcdHistoryExp = array();
        $lcdCmcCovPolicy = array();
        $lcdUtilGuide = array();
        $hcpcCodeId = array();
        $hcpcCodeVersion = array();
        $hcpcLongDescription = array();
        $hcpcShortDescription = array();
        $icd9CodeId = array();
        $icd9CodeVersion = array();
        $icd9Description = array();
        $lcdRevEffDate = array();

        /* Fetch LCD relaed data from LCD table */
        $result = $this->search_model->getLCDById($lcdIdNo);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $lcdId[] = $row['lcd_id'];
                $lcdTitle[] = $row['title'];
                $lcdIndication[] = $row['indication'];
                $lcdDocReqs[] = $row['doc_reqs'];
                $lcdAdvMeeting[] = $row['adv_meeting'];
                $lcdHistoryExp[] = $row['history_exp'];
                $lcdCmcCovPolicy[] = $row['cms_cov_policy'];
                $lcdUtilGuide[] = $row['util_guide'];
                $lcdRevEffDate[] = $row['rev_eff_date'];
            }
        }
        /* Fetch HCPC Code relaed data from HCPC lookup table */
        $result = $this->search_model->getHCPCDatabyLCDId($lcdIdNo);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $hcpcCodeId[] = $row['hcpc_code_id'];
                $hcpcCodeVersion[] = $row['hcpc_code_version'];
                $hcpcLongDescription[] = $row['long_description'];
                $hcpcShortDescription[] = $row['long_description'];
            }
        }
        /* Fetch LCD relaed data from LCD table */
        $result = $this->search_model->getIDC9DatabyLCDId($lcdIdNo);
        if (count($result) > 0) {
            foreach ($result as $row) {
                $icd9CodeId[] = $row['icd9_code_id'];
                $icd9CodeVersion[] = $row['icd9_code_version'];
                $icd9Description[] = $row['description'];
            }
        }
        /* Load view */
        $this->load->view('lcd_view', array(
            'lcdId' => $lcdId,
            'lcdTitle' => $lcdTitle,
            'lcdIndication' => $lcdIndication,
            'lcdDocReqs' => $lcdDocReqs,
            'lcdAdvMeeting' => $lcdAdvMeeting,
            'lcdHistoryExp' => $lcdHistoryExp,
            'lcdCmcCovPolicy' => $lcdCmcCovPolicy,
            'lcdUtilGuide' => $lcdUtilGuide,
            'lcdRevEffDate' => $lcdRevEffDate,
            'hcpcCodeId' => $hcpcCodeId,
            'hcpcCodeVersion' => $hcpcCodeVersion,
            'hcpcLongDescription' => $hcpcLongDescription,
            'hcpcShortDescription' => $hcpcShortDescription,
            'icd9CodeId' => $icd9CodeId,
            'icd9CodeVersion' => $icd9CodeVersion,
            'icd9Description' => $icd9Description));
    }

    function getStateNameByStateId($stateId) {
        $result = $this->search_model->getStateNameByStateId($stateId);
        if (count($result) > 0 && count($result == 1)) {
            return $result[0]['description'];
        }
        else
            return $stateId;
    }

    function getLCDTitle($lcdId) {
        return $this->search_model->getLCDTitleById($lcdId);
    }

    function getUniqueResultArray($result) {
        if ($result != '') {
            foreach ($result as $k => $v) {
                $new[$k] = serialize($v);
            }
            $unique = array_unique($new);
            $result = null;
            foreach ($unique as $k => $v)
                $result[$k] = unserialize($v);
        }
        return $result;
    }

    function getIntersectionArray($result1, $result2) {
        $result = array();
        if (count($result1) > count($result2)) {
            $temp = array();
            $temp = $result1;
            $result1 = null;
            $result1 = $result2;
            $result2 = null;
            $result2 = $temp;
        }
        foreach ($result1 as $row) {
            if (in_array($row, $result2)) {
                $result[] = $row;
            }
        }
        return $result;
    }

}

?>